package com.bluebench.bigul.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import com.bluebench.bigul.domain.Element;
import com.bluebench.bigul.domain.Position;
import com.bluebench.bigul.domain.Types;
import com.bluebench.bigul.exception.GraphicInitializationException;
import com.bluebench.bigul.service.processor.GenericProcessor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;


public class DocumentGenerator {

	private Document document;
	private String outputpath;
	private static PdfWriter writer;
	
	public void generateDocument(String output,Element element) throws FileNotFoundException, SecurityException, IllegalArgumentException, DocumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException, GraphicInitializationException{
		this.outputpath = output;
		generateDocument(element);
		document.close();
	}
	
	public void generateDocument(Element e) throws FileNotFoundException, DocumentException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException, GraphicInitializationException{
	
		Types etype = Types.getType(e.getType());
		switch(etype){
			case TEMPLATE:
				document =  new Document();
				writer = PdfWriter.getInstance(document,new FileOutputStream(outputpath));
				String user = e.getUser();
				String owner = e.getOwner();
				
				if(user!=null && owner!=null){
					byte[] userPassword = e.getUser().getBytes();
					byte[] ownerPassword = e.getOwner().getBytes();
					boolean allowPrint = e.isAllowPrint();
					boolean allowCopy = e.isAllowCopy();
					int permissions = getPermissions(allowPrint,allowCopy);				
					writer.setEncryption(userPassword, ownerPassword, permissions, PdfWriter.ENCRYPTION_AES_128);
				}
				
				document.open();
				List<Element> pages = e.getElements();
				for(Element page : pages){
					generateDocument(page);
				}
				break;
			case PAGE:
				document.newPage();
				List<Element> elements = e.getElements();
				for(Element element : elements){
					generateDocument(element);					
				}
				break;
			default:
				e.setProcessor(GenericProcessor.getInstance(GenericProcessor.ELEMENT_PROCESSOR));
				Object itextObject = e.process();				
				Position po = e.getPosition();
				 
				if(po!=null){
					//***** Move to a different method *******
					ColumnText ct = new ColumnText(writer.getDirectContent());
					ct.setSimpleColumn(po.leftX,po.leftY,po.rightX,po.rightY);
					if(itextObject instanceof com.itextpdf.text.Element){
						ct.addElement((com.itextpdf.text.Element)itextObject);
						ct.go();
					}
					//****************************************
				}else{
				 if(itextObject instanceof com.itextpdf.text.Element){
					com.itextpdf.text.Element itextElement = (com.itextpdf.text.Element)itextObject;
					document.add(itextElement);
				 }
				}
				
		}				
		
		
	}

	private int getPermissions(boolean allowPrint, boolean allowCopy) {
		Integer permission=0;
		if(allowPrint){
			permission  = PdfWriter.ALLOW_PRINTING;
		}
		if(allowCopy){
			permission = permission | PdfWriter.ALLOW_COPY;
		}
		return permission;
	}

	public static PdfWriter getWriter() {
		return writer;
	}

	public static void setWriter(PdfWriter writer) {
		DocumentGenerator.writer = writer;
	}	
	
}
